home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBGRX100.ARJ / CIRCLE.C < prev    next >
Text File  |  1992-04-10  |  4KB  |  167 lines

  1. /** 
  2.  ** CIRCLE.C 
  3.  **
  4.  **  Copyright (C) 1992, Csaba Biegl
  5.  **    820 Stirrup Dr, Nashville, TN, 37221
  6.  **    csaba@vuse.vanderbilt.edu
  7.  **
  8.  **  This file is distributed under the terms listed in the document
  9.  **  "copying.cb", available from the author at the address above.
  10.  **  A copy of "copying.cb" should accompany this file; if not, a copy
  11.  **  should be available from where this file was obtained.  This file
  12.  **  may not be distributed without a verbatim copy of "copying.cb".
  13.  **  You should also have received a copy of the GNU General Public
  14.  **  License along with this program (it is in the file "copying");
  15.  **  if not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  16.  **  Cambridge, MA 02139, USA.
  17.  **
  18.  **  This program is distributed in the hope that it will be useful,
  19.  **  but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.  **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.  **  GNU General Public License for more details.
  22.  **/
  23.  
  24. #include "grx.h"
  25. #include "libgrx.h"
  26. #include "clipping.h"
  27.  
  28. #define  ulong  unsigned long
  29.  
  30. static void drawellip(int xc,int yc,int xa,int ya,int c,int filled)
  31. {
  32.     ulong sqr,dx2,xa2;
  33.     int   x1,x2,y1,y2;
  34.     int   dx,dy,dxprev;
  35.     int   small;
  36.     MOUSE_FLAG;
  37.  
  38.     if(xa < 0) xa = (-xa);
  39.     if(ya < 0) ya = (-ya);
  40.     x1 = xc - xa; y1 = yc - ya;
  41.     x2 = xc + xa; y2 = yc + ya;
  42.     CLIPBOXTEST(CURC,x1,y1,x2,y2);
  43.     MOUSE_BLOCK(CURC,x1,y1,x2,y2);
  44.     if(xa <= 1) {
  45.         if(xa > 0) {
  46.         dy = (int)((((ulong)ya * 7L) + 5L) / 10L);
  47.         y2 = yc + dy;
  48.         y1 = yc - dy;
  49.         GrVLine(xc-1,y1,y2,c);
  50.         GrVLine(xc+1,y1,y2,c);
  51.         }
  52.         if(filled || (xa == 0) || (y1 == y2))
  53.         GrVLine(xc,yc-ya,yc+ya,c);
  54.         else {
  55.         GrVLine(xc,yc-ya,y1,c);
  56.         GrVLine(xc,yc+ya,y2,c);
  57.         }
  58.         MOUSE_UNBLOCK();
  59.         return;
  60.     }
  61.     if(ya <= 1) {
  62.         if(ya > 0) {
  63.         dx = (int)((((ulong)xa * 7L) + 5L) / 10L);
  64.         x2 = xc + dx;
  65.         x1 = xc - dx;
  66.         GrHLine(x1,x2,yc-1,c);
  67.         GrHLine(x1,x2,yc+1,c);
  68.         }
  69.         if(filled || (ya == 0) || (x1 == x2))
  70.         GrHLine(xc-xa,xc+xa,yc,c);
  71.         else {
  72.         GrHLine(xc-xa,x1,yc,c);
  73.         GrHLine(x2,xc+xa,yc,c);
  74.         }
  75.         MOUSE_UNBLOCK();
  76.         return;
  77.     }
  78.     sqr = (ulong)(xa + 1) * (ulong)ya;
  79.     if(sqr < 65536L) {
  80.         small = TRUE;
  81.         dx2      = (ulong)xa * (ulong)ya;
  82.         sqr  *= dx2;
  83.         xa2      = (ulong)xa * (ulong)(xa + 1);
  84.     }
  85.     else {
  86.         small = FALSE;
  87.         dx2      = (ulong)ya;
  88.         sqr      = (ulong)ya * (ulong)(ya + 1);
  89.     }
  90.     dx = dxprev = xa;
  91.     dy = 0;
  92.     if(filled)
  93.         GrHLine(xc-dx,xc+dx,yc,c);
  94.     else {
  95.         GrPlot(xc-dx,yc,c);
  96.         GrPlot(xc+dx,yc,c);
  97.     }
  98.     while(++dy <= ya) {
  99.         if(small) {
  100.         if(dx) {
  101.             sqr -= (ulong)(dy + dy - 1) * xa2;
  102.             dx2 = (dx2 + sqr/dx2) / 2L;
  103.             dx  = (int)(dx2 / (ulong)ya);
  104.         }
  105.         }
  106.         else {
  107.         if(dx) {
  108.             sqr -= (ulong)(dy + dy - 1);
  109.             dx2 = (dx2 + sqr/dx2) / 2L;
  110.             dx  = (int)((dx2 * (ulong)xa) / (ulong)ya);
  111.         }
  112.         }
  113.         x1 = xc - dx;
  114.         x2 = xc + dx;
  115.         y1 = yc - dy;
  116.         y2 = yc + dy;
  117.         if(!filled && ((dxprev - dx) > 1)) {
  118.         if((dxprev - dx) > 2) {
  119.             GrLine(xc-dxprev+1,y1+1,x1-1,y1,c);
  120.             GrLine(x2+1,y1,xc+dxprev-1,y1+1,c);
  121.             GrLine(xc-dxprev+1,y2-1,x1-1,y2,c);
  122.             GrLine(x2+1,y2,xc+dxprev-1,y2-1,c);
  123.         }
  124.         else {
  125.             GrPlot(x1-1,y1+1,c);
  126.             GrPlot(x2+1,y1+1,c);
  127.             GrPlot(x1-1,y2-1,c);
  128.             GrPlot(x2+1,y2-1,c);
  129.         }
  130.         }
  131.         dxprev = dx;
  132.         if(filled || (dy == ya)) {
  133.         GrHLine(x1,x2,y1,c);
  134.         GrHLine(x1,x2,y2,c);
  135.         continue;
  136.         }
  137.         else {
  138.         GrPlot(x1,y1,c);
  139.         GrPlot(x2,y1,c);
  140.         GrPlot(x1,y2,c);
  141.         GrPlot(x2,y2,c);
  142.         }
  143.     }
  144.     MOUSE_UNBLOCK();
  145. }
  146.  
  147. void GrCircle(int xc,int yc,int r,int c)
  148. {
  149.     drawellip(xc,yc,r,r,c,FALSE);
  150. }
  151.  
  152. void GrFilledCircle(int xc,int yc,int r,int c)
  153. {
  154.     drawellip(xc,yc,r,r,c,TRUE);
  155. }
  156.  
  157. void GrEllipse(int xc,int yc,int xa,int ya,int c)
  158. {
  159.     drawellip(xc,yc,xa,ya,c,FALSE);
  160. }
  161.  
  162. void GrFilledEllipse(int xc,int yc,int xa,int ya,int c)
  163. {
  164.     drawellip(xc,yc,xa,ya,c,TRUE);
  165. }
  166.  
  167.